package DBLayer;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import ModelLayer.*;

public class DBPartial_Material {

	private Connection con;
	
	public DBPartial_Material()
	{
		con = DbConnection1.getInstance().getDBcon();
	}
	
	private ArrayList<Partial_Material> miscWhere(String wClause, boolean retrieveAssociation)
	{
    	ResultSet results;
	    ArrayList<Partial_Material> list = new ArrayList<Partial_Material>();	
		
	    String query =  buildQuery(wClause);
  
        try
        { // read the material from the database
        	Statement stmt = con.createStatement();
        	stmt.setQueryTimeout(5);
        	results = stmt.executeQuery(query);
        	
        	while( results.next() )
        	{
        		Partial_Material paObj = new Partial_Material();
        		paObj = buildPartailMaterial(results);	
                list.add(paObj);
        	}//end while
        	stmt.close();     
        
        }//slut try	
	 	
        catch(Exception e)
        {
	 		System.out.println("Query exception - select: "+e);
			e.printStackTrace();
	 	}
		return list;
	}
    
	//singleWhere is used when we only select one material
	private Partial_Material singleWhere(String wClause, boolean retrieveAssociation)
	{
		ResultSet results;
		Partial_Material paObj = new Partial_Material();
                
	    String query =  buildQuery(wClause);
        System.out.println(query);
		try
		{ // read the employee from the database
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		results = stmt.executeQuery(query);
	 		
	 		if( results.next() )
	 		{
	 			paObj = buildPartailMaterial(results);
                stmt.close();
	 		}
            else
            { //no Material was found
            	paObj = null;
            }
		}//end try	
	 	catch(Exception e)
	 	{
	 		System.out.println("Query exception: "+e);
	 	}
		return paObj;
	}
	
	private String buildQuery(String wClause)
    {
        // This string query needs to be revised.
        String query = "Select idMaterial, idPartialAssignment, amount";

        if (wClause.length() > 0)
        {
            query = query + " WHERE " + wClause;
        }
        return query;
    }
	
	private Partial_Material buildPartailMaterial(ResultSet results)
    {
        Partial_Material paObj = new Partial_Material();
        try
        {
        	paObj.setIdMaterial(results.getString("idMaterial"));
            paObj.setIdPartialAssignment(results.getString("idPartialMaterial"));
            paObj.setAmount(results.getDouble("amount"));
            paObj.setMatObj(new Material(results.getString("idMaterial")));
            paObj.setPaObj(new PartialAssignment(results.getString("idPartialAssignment")));
        }
        catch (Exception e)
        {
            System.out.println("error in building the Partial_Material object");
        }
        return paObj;
    }
}
